<template>
	<view class="prizeDraw">
		<view class="trun_big_box">
			<almost-lottery ref="almostLottery" :prizeList="prizeList" :prizeIndex="prizeIndex"
				@reset-index="prizeIndex = -1" @draw-before="handleDrawBefore" @draw-start="handleDrawStart"
				@draw-end="handleDrawEnd" @finish="handleDrawFinish" v-if="prizeList.length" />
			<view class="draw_btn" @tap="drawFunc">
				<image src="../../static/images/icon/lotteryBtn.png" mode=""></image>
			</view>
		</view>
		<view class="prizeRecord">
			<view class="prizeRecordTitle">
				获奖记录
			</view>
			<view class="prizeRecordList">
				<view class="scrolling-list">
					<view v-for="(item, index) in recordList" :key="index" class="list-item">
						恭喜{{ item.name }}获得{{item.value}}</view>
				</view>
			</view>
		</view>
		<!-- <view class="check_money">
			<view class="yellow_btn" @tap="goCheck">
				查看余额
			</view>
		</view> -->
		<u-popup :show="resultShow" mode="center" @close="resultClose" :round="10">
			<view class="resultBox">
				<view class="result_popup_title">
					恭喜你抽中 {{endPrizeMoney}}!
				</view>
				<view class="yellow_btn" @tap="goCheck">查看</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import AlmostLottery from '@/uni_modules/almost-lottery/components/almost-lottery/almost-lottery.vue'
	export default {
		components: {
			AlmostLottery
		},
		data() {
			return {
				// 以下是奖品配置数据
				// 奖品数据
				prizeList: [{
						prizeId: 1,
						prizeName: "1元钱",
						money: 1
					},
					{
						prizeId: 2,
						prizeName: "1元钱",
						money: 1
					},
					{
						prizeId: 3,
						prizeName: "1元钱",
						money: 1
					},
					{
						prizeId: 4,
						prizeName: "1元钱",
						money: 1
					},
					{
						prizeId: 5,
						prizeName: "1元钱",
						money: 1
					},
					{
						prizeId: 6,
						prizeName: "1元钱",
						money: 1
					},
				],
				// 中奖下标
				prizeIndex: -1,
				recordList: [{
						name: '张*强',
						value: '2斤空心菜'
					},
					{
						name: '李*鹏',
						value: '20减5的优惠券'
					},
					{
						name: '王*子',
						value: '10元钱'
					},
					{
						name: '张*大',
						value: '漫花庄园一日游'
					},
					{
						name: '王*上',
						value: '8元钱'
					},
				],
				curScrollTop: 0,
				timer: null,
				curCode: '',
				endPrizeMoney: '',
				resultShow: false,
				isGetPrize: false
			}
		},
		onLoad(val) {
			// this.curCode = '20240321214456eJkRQM5';
			// this.startScrolling();
			// this.getDrawConfig();
			if (val.scene) {
				this.curCode = val.scene.replace('code%3D', '');
				this.startScrolling();
				this.getDrawConfig();
			} else {
				this.$helper.toast('未获取到正确的code');
			}

		},
		methods: {
			goCheck() {
				uni.navigateTo({
					url: '/pages/prizeRecords/prizeRecords'
				})
			},
			resultClose() {
				this.resultShow = false;
			},
			startScrolling() {
				this.timer = setInterval(() => {
					this.curScrollTop++;
				}, 500); // Adjust the interval as needed (in milliseconds)
			},
			initScroll() {
				console.log('触底了', this.curScrollTop);
				this.curScrollTop = 0;
			},

			getDrawConfig() {
				this.$api.getDrawConfig({
					code: this.curCode
				}).then(res => {
					console.log(res, '当前抽奖池');
					if (res.code == 1) {
						let data = res.data.lists;
						this.prizeList = data.map(item => {
							return {
								prizeId: item.id,
								prizeName: item.name,
								money: item.money
							}
						})
					} else {
						this.$helper.toast(res.msg);
					}
				})
			},
			// 本次抽奖开始之前
			handleDrawStart(callback) {
				// 这里需要处理你抽奖之前的逻辑
				// 请查看示例项目中的代码
				// 必须调用 callback 并传递一个布尔值，布尔值为 true 时，转盘才会开始旋转
				let flag = true

				callback(flag)
			},
			// 本次抽奖开始
			handleDrawStart() {
				// 这里需要处理你的抽奖逻辑，并得出中奖物品的 prizeIndex
				// 请查看示例项目中的代码
			},
			// 本次抽奖结束
			handleDrawEnd() {
				// 完成抽奖后，这里处理你拿到结果后的逻辑
				// 请查看示例项目中的代码
				// this.$helper.toast(`恭喜你抽中${this.endPrizeMoney}`);
				if (this.isGetPrize) {
					this.resultShow = true;
				} else {
					this.$helper.toast('谢谢参与')
				}
			},
			// 抽奖转盘绘制完成
			handleDrawFinish(res) {
				// 抽奖转盘准备就绪后，这里处理你的逻辑
				// 请查看示例项目中的代码
				// console.log('抽奖转盘绘制完成', res)
			},
			handleDrawBefore(res) {
				this.$api.drawPrize({
					code: this.curCode
				}).then(res => {
					if (res.code == 1) {
						this.prizeList.forEach((item, index) => {
							if (item.prizeId == res.data.id) {
								this.prizeIndex = index;
								this.endPrizeMoney = res.data.name;
								if (res.data.type == 'none') {
									this.isGetPrize = false
								} else {
									this.isGetPrize = true;
								}
							}
						})
					} else {
						this.$helper.toast(res.msg);
					}
				})
			},
			drawFunc() {
				this.$refs.almostLottery.handleActionStart();
				console.log(this.$refs, '当前refs');
				// this.$refs.almostLottery
			}
		}
	};
</script>

<style lang="scss">
	.prizeDraw {
		width: 100vw;
		height: 100vh;
		background-size: 100% 100%;
		background-image: url('https://dyx.zhinh.cn/uploads/draw/lotteryPageBg%20(1).png');

		.trun_big_box {
			padding-top: 180rpx;
			position: relative;

			.draw_btn {
				position: absolute;
				bottom: -190rpx;
				right: 0;
				left: 0;
				width: 420rpx;
				height: 88rpx;
				margin: auto;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.prizeRecord {
			position: absolute;
			bottom: 50rpx;
			left: 0;
			right: 0;
			margin: auto;

			.prizeRecordTitle {
				color: #ffefaf;
				font-weight: bold;
				text-align: center;
				font-size: 36rpx;
			}
		}

		.scrolling-list {
			height: 130rpx;
			/* Adjust the height according to your design */
			overflow: hidden;
			padding-bottom: 20rpx;

		}

		@-webkit-keyframes rowup {
			0% {
				-webkit-transform: translate3d(0, 0, 0);
				transform: translate3d(0, 0, 0);
			}

			100% {
				-webkit-transform: translate3d(0, -210rpx, 0);
				transform: translate3d(0, -210rpx, 0);
			}
		}

		@keyframes rowup {
			0% {
				-webkit-transform: translate3d(0, 0, 0);
				transform: translate3d(0, 0, 0);
			}

			100% {
				-webkit-transform: translate3d(0, -250rpx, 0);
				transform: translate3d(0, -250rpx, 0);
			}
		}

		.list-item {
			opacity: 1;
			transition: opacity 0.5s ease-in-out;
			color: #f7c592;
			font-size: 26rpx;
			text-align: center;
			margin-bottom: 14rpx;
			-webkit-animation: 10s rowup linear infinite normal;
			animation: 10s rowup linear infinite normal;
			animation-delay: 2s;
		}

		.resultBox {
			width: 70vw;
			padding: 20rpx;
			position: relative;
			background-color: #ea272e;
			border-radius: 30rpx;

			.result_popup_title {
				padding-top: 20rpx;
				text-align: center;
				margin-bottom: 100rpx;
				color: #f7c592;
				font-weight: bold;
			}

			.yellow_btn {}
		}

	}
</style>